package com.yh.serviceimpl;

import com.yh.dao.StudentDao;
import com.yh.dao.TeacherDao;
import com.yh.daoimpl.StudentDaoImpl;
import com.yh.daoimpl.TeacherDaoImpl;
import com.yh.javabean.Student;
import com.yh.service.TeacherService;

import java.util.ArrayList;
import java.util.Random;

/**
 * @Auther: Liang
 * @Date: 2024/4/2 - 04 - 02 - 10:30
 * @Description: com.sms.serviceimpl
 * @version: 1.0
 */
public class TeacherServiceImpl implements TeacherService {
    StudentDao studentDao = new StudentDaoImpl();
    TeacherDao teacherDao= new TeacherDaoImpl();
    @Override
    public ArrayList<Student> showMyStudent(int tid) {
          return studentDao.selectBytid(tid);
    }

    @Override
    public void upLoadScore(int sid, double score) {
          studentDao.updateScoreBysid(sid, score);
    }

    @Override
    public String unluckyWretch(int tid) {
        //1.查询出tid所有状态为0(为被选)的学生列表
        ArrayList<Student> allstudent = studentDao.selectByTidAndStatus(tid, 0);
        //2.随机数工具类，随机生成索引选择学生
        Random r =new Random();
        int index = r.nextInt(allstudent.size());
        //3.通过索引查找倒霉的人
        Student student = allstudent.get(index);
        //4.将倒霉人数据库对应的字段设置为已选
        studentDao.updatStatusBysid(student.getSid(),1);
        //5.判断当前倒霉人是否为最后一个
        if(allstudent.size()==1){
            //将当前教师所有学生设置为未选
            studentDao.updatStatusBytid(tid,0);
        }
        return student.getName();
    }


    @Override
    public void restPassword(int tid, String password) {
             teacherDao.updatePasswordBytid(tid, password);
    }
}
